
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    
<div class="archive_info">
<h1>JavaScript <span class="color_h1">switch</span> 语句</h1>
<hr/>
<p class="intro">switch 语句用于基于不同的条件来执行不同的动作。</p>
<hr/>
<h2>JavaScript switch 语句</h2>
<p>请使用 switch 语句来选择要执行的多个代码块之一。</p>
<div class="example">
<h2 class="example">语法</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">switch</span><span class="hl-brackets">(</span><span class="hl-identifier">n</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">case</span><span class="hl-code"> </span><span class="hl-number">1</span><span class="hl-code">:
        执行代码块 </span><span class="hl-number">1</span><span class="hl-code">
</span><span class="hl-reserved">break</span><span class="hl-code">;
    </span><span class="hl-reserved">case</span><span class="hl-code"> </span><span class="hl-number">2</span><span class="hl-code">:
        执行代码块 </span><span class="hl-number">2</span><span class="hl-code">
</span><span class="hl-reserved">break</span><span class="hl-code">;
    </span><span class="hl-reserved">default</span><span class="hl-code">:
        与 </span><span class="hl-reserved">case</span><span class="hl-code"> </span><span class="hl-number">1</span><span class="hl-code"> 和 </span><span class="hl-reserved">case</span><span class="hl-code"> </span><span class="hl-number">2</span><span class="hl-code"> 不同时执行的代码
</span><span class="hl-brackets">}</span></div>
</div>
</div>
<p>工作原理：首先设置表达式 <i>n</i>（通常是一个变量）。随后表达式的值会与结构中的每个 case 的值做比较。如果存在匹配，则与该 case 关联的代码块会被执行。请使用 <strong>break </strong>来阻止代码自动地向下一个 case 运行。</p>
<div class="example">
<h2 class="example">实例</h2>
<p class="example">显示今天的星期名称。请注意 Sunday=0, Monday=1, Tuesday=2, 等等：</p>
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">d</span><span class="hl-code">=</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-builtin">Date</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-identifier">getDay</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">; 
</span><span class="hl-reserved">switch</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">d</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">case</span><span class="hl-code"> </span><span class="hl-number">0</span><span class="hl-code">:</span><span class="hl-identifier">x</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">今天是星期日</span><span class="hl-quotes">"</span><span class="hl-code">; 
  </span><span class="hl-reserved">break</span><span class="hl-code">; 
  </span><span class="hl-reserved">case</span><span class="hl-code"> </span><span class="hl-number">1</span><span class="hl-code">:</span><span class="hl-identifier">x</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">今天是星期一</span><span class="hl-quotes">"</span><span class="hl-code">; 
  </span><span class="hl-reserved">break</span><span class="hl-code">; 
  </span><span class="hl-reserved">case</span><span class="hl-code"> </span><span class="hl-number">2</span><span class="hl-code">:</span><span class="hl-identifier">x</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">今天是星期二</span><span class="hl-quotes">"</span><span class="hl-code">; 
  </span><span class="hl-reserved">break</span><span class="hl-code">; 
  </span><span class="hl-reserved">case</span><span class="hl-code"> </span><span class="hl-number">3</span><span class="hl-code">:</span><span class="hl-identifier">x</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">今天是星期三</span><span class="hl-quotes">"</span><span class="hl-code">; 
  </span><span class="hl-reserved">break</span><span class="hl-code">; 
  </span><span class="hl-reserved">case</span><span class="hl-code"> </span><span class="hl-number">4</span><span class="hl-code">:</span><span class="hl-identifier">x</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">今天是星期四</span><span class="hl-quotes">"</span><span class="hl-code">; 
  </span><span class="hl-reserved">break</span><span class="hl-code">; 
  </span><span class="hl-reserved">case</span><span class="hl-code"> </span><span class="hl-number">5</span><span class="hl-code">:</span><span class="hl-identifier">x</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">今天是星期五</span><span class="hl-quotes">"</span><span class="hl-code">; 
  </span><span class="hl-reserved">break</span><span class="hl-code">; 
  </span><span class="hl-reserved">case</span><span class="hl-code"> </span><span class="hl-number">6</span><span class="hl-code">:</span><span class="hl-identifier">x</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">今天是星期六</span><span class="hl-quotes">"</span><span class="hl-code">; 
  </span><span class="hl-reserved">break</span><span class="hl-code">; 
</span><span class="hl-brackets">}</span></div>
</div>
<p><em>x</em> 的运行结果：</p>
<div class="example_code">
<script>
var d=new Date().getDay();
switch (d)
{
  case 0:x="今天是星期日";
  break;
  case 1:x="今天是星期一";
  break;
  case 2:x="今天是星期二";
  break;
  case 3:x="今天是星期三";
  break;
  case 4:x="今天是星期四";
  break;
  case 5:x="今天是星期五";
  break;
  case 6:x="今天是星期六";
  break;
}
document.write(x);
</script>
</div>
<br/><a class="tryitbtn"  target="_blank">尝试一下 »</a>
</div>
<h2>default 关键词</h2>
<p>请使用 default 关键词来规定匹配不存在时做的事情：</p>
<div class="example">
<h2 class="example">实例</h2>
<p class="example">如果今天不是星期六或星期日，则会输出默认的消息：</p>
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">d</span><span class="hl-code">=</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-builtin">Date</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-identifier">getDay</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-reserved">switch</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">d</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">case</span><span class="hl-code"> </span><span class="hl-number">6</span><span class="hl-code">:</span><span class="hl-identifier">x</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">今天是星期六</span><span class="hl-quotes">"</span><span class="hl-code">;
    </span><span class="hl-reserved">break</span><span class="hl-code">;
    </span><span class="hl-reserved">case</span><span class="hl-code"> </span><span class="hl-number">0</span><span class="hl-code">:</span><span class="hl-identifier">x</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">今天是星期日</span><span class="hl-quotes">"</span><span class="hl-code">;
    </span><span class="hl-reserved">break</span><span class="hl-code">;
    </span><span class="hl-reserved">default</span><span class="hl-code">:
    </span><span class="hl-identifier">x</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">期待周末</span><span class="hl-quotes">"</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-builtin">document</span><span class="hl-code">.</span><span class="hl-identifier">getElementById</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">demo</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-identifier">innerHTML</span><span class="hl-code">=</span><span class="hl-identifier">x</span><span class="hl-code">;</span></div>
</div>
<p> <em>x</em> 的运行结果：</p>
<div class="example_code">
<script>
var x;
var d=new Date().getDay();
switch (d)
{
  case 6:x="今天是星期六";
  break;
  case 0:x="今天是星期日";
  break;
  default:
  x="期待周末";
}
document.write(x);
</script>
</div>
<br/><a class="tryitbtn"  target="_blank">尝试一下 »</a>
</div> <div class="scroll-top"><a >返回顶部</a></div>
</div>

    </body>
    </html>
    